#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Student{
public:
    Student(const char * name,int score)
    : _name(new char[strlen(name) + 1]())
    , _score(score)
    {
        strcpy(_name,name);
        cout << "Student的构造函数" << endl;
    }

public:
    Student(const Student & rhs)
    : _name(new char[strlen(rhs._name) + 1]())
    , _score(rhs._score)
    {
        strcpy(_name,rhs._name);
        cout << "Student的拷贝构造" << endl;
    }

    Student & operator=(const Student & rhs){
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name,rhs._name);
            _score = rhs._score;
            cout << "Student的赋值运算符函数" << endl;
        }
        return *this;
    }
public:
    void * operator new(size_t sz){
        cout << "Student的operator new" << endl;
        return malloc(sz);
    }
    void operator delete(void * p){
        cout << "Student的operator delete" << endl;
        free(p);
    }

public:

    void print() const{
        cout << "name:" << _name << endl
            << "score:" << _score << endl;
    }
private:
    ~Student(){
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
        cout << "Student的析构函数" << endl;
    }
private:
    char * _name;
    int _score;
};

#if 1
void test0(){
    //1.调用operator new库函数申请未类型化的空间
    //2.调用构造函数
    //3.返回一个相应类型的指针
    Student * pstu = new Student("Jack",95);
    pstu->print();
    delete pstu;

    /* Student stu("Bob",90); */
    /* stu.print(); */
}
#endif

#if 0
void test1(){
    Student stu("Bob",90);
    stu.print();
}
#endif

int main(void){
    test0();
    /* test1(); */
    return 0;
}
